﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using SonCa.SocialNetwork.Web.Friends.IPresenters;
using Core.Common;
using SonCa.SocialNetwork.Web.HelperClass;
using System.ComponentModel.Composition;
using Core.BusinessLogic.Interfaces;
using Core.Interfaces;
using Core.DataAccess;

namespace SonCa.SocialNetwork.Web.Friends.Presenters
{
    public class AllFriendsPresenter
    {
        private IAllFriendsPresenter _view;
        private List<FriendRequestHelper> _friendRequestHelper;
        [Import]
        private IUserSession _userSession;        
        [Import]
        private ISchoolService _schoolService;
        [Import]
        private ILocationService _locationService;
        [Import]
        private IWorkPlaceService _workplaceService;
        [Import]
        private IFriendService _friendService;
        [Import]
        private IAccountService _accountService;

        public void Init(IAllFriendsPresenter view)
        {
            _view = view;
            MEFManager.Compose(this);
            _friendRequestHelper = new List<FriendRequestHelper>();

            List<School> lstschool = _schoolService.GetSchoolsByAccountID(_userSession.CurrentUser.AccountID);
            if (lstschool.Count > 0)
            {
                _schoolService.GetAllFriendsSchoolByAccountID(_userSession.CurrentUser.AccountID, lstschool.Last().SchoolName).ForEach(n => _friendRequestHelper.Add(new FriendRequestHelper
                {
                    AccountID = n.AccountID,
                    FullName = n.LastName + " " + n.MidName + " " + n.FirstName,
                    ImageUrl = string.Format("../Images/ProfileAvatar/ProfileImage.aspx?AccountID={0}", n.AccountID)
                }));
            }

            List<Location> lstLocation = _locationService.GetLocationListByAccountID(_userSession.CurrentUser.AccountID);
            if (lstLocation.Count > 0)
            {
                _locationService.GetAllFriendsLocationByAccountID(_userSession.CurrentUser.AccountID, lstLocation.Last().LocationName).ForEach(n => _friendRequestHelper.Add(new FriendRequestHelper
                {
                    AccountID = n.AccountID,
                    FullName = n.LastName + " " + n.MidName + " " + n.FirstName,
                    ImageUrl = string.Format("../Images/ProfileAvatar/ProfileImage.aspx?AccountID={0}", n.AccountID)
                }));
            }

            List<WorkPlace> lstWorkplace = _workplaceService.GetWorkPlaceListByAccountID(_userSession.CurrentUser.AccountID);
            if (lstWorkplace.Count > 0)
            {
                _workplaceService.GetAllFriendsWorkplaceByAccountID(_userSession.CurrentUser.AccountID, lstWorkplace.Last().CompanyName).ForEach(n => _friendRequestHelper.Add(new FriendRequestHelper
                {
                    AccountID = n.AccountID,
                    FullName = n.LastName + " " + n.MidName + " " + n.FirstName,
                    ImageUrl = string.Format("../Images/ProfileAvatar/ProfileImage.aspx?AccountID={0}", n.AccountID)
                }));
            }

            // get random friends account and filter
            List<Account> lstRandomAccount = _accountService.GetRandomListAccount();
            foreach (FriendRequestHelper fr in _friendRequestHelper)
            {
                int index = lstRandomAccount.FindIndex(n => n.AccountID == fr.AccountID);
                if (index >= 0)
                    lstRandomAccount.RemoveAt(index);
            }

            int pos = lstRandomAccount.FindIndex(n => n.AccountID == _userSession.CurrentUser.AccountID);
            if (pos >= 0)
                lstRandomAccount.RemoveAt(pos);

            lstRandomAccount.ForEach(n => _friendRequestHelper.Add(new FriendRequestHelper
            {
                AccountID = n.AccountID,
                FullName = n.LastName + " " + n.MidName + " " + n.FirstName,
                ImageUrl = string.Format("../Images/ProfileAvatar/ProfileImage.aspx?AccountID={0}", n.AccountID)
            }));

            // filter friend
            List<Friend> lstFriends = _friendService.GetFriendsByAccountID(_userSession.CurrentUser.AccountID);
            foreach(Friend fr in lstFriends)
            {
                int index = _friendRequestHelper.FindIndex(n => n.AccountID == fr.MyFriendsAccountID || n.AccountID == fr.AccountID);                
                if(index >= 0)
                    _friendRequestHelper.RemoveAt(index);
            }            

            _view.LoadAllFriend(_friendRequestHelper);
        }
    }
}